/*
 * @Author: 彭林
 * @Date: 2021-05-14 18:03:19
 * @LastEditTime: 2021-07-01 14:59:49
 * @LastEditors: 彭林
 * @Description:
 * @FilePath: /src/saas-app/app/api/v1/card.go
 */
package v1

import (
	"saas-app/app/define"
	"saas-app/app/service"
	"saas-app/library/response"

	"github.com/gogf/gf/net/ghttp"
)

func GetCardList(r *ghttp.Request) {

	var (
		pagingParams *define.PagingParams
		campus_id    = r.GetCtxVar("campus_id").Int()
	)

	if err := r.Parse(&pagingParams); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result, total := service.CardService().GetCardList(campus_id, pagingParams)

	if pagingParams.Page > 0 && pagingParams.Limit > 0 {
		response.JsonExit(
			r, 0, "ok",
			response.Paging(result, total, pagingParams.Page, pagingParams.Limit),
		)
	} else {
		response.JsonExit(r, 0, "ok", result)
	}
}

func UpdateCard(r *ghttp.Request) {

	card_id := r.GetQueryUint("card_id")

	result := service.CardService().UpdateCard(card_id, r.GetFormMap())

	response.JsonExit(r, 0, "ok", result)
}

func CreateCard(r *ghttp.Request) {

	var (
		data *define.CardRequest
	)

	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	service.CardService().CreateCard(data)

	response.JsonExit(r, 0, "ok", nil)
}
